define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'logistics/index' + location.search,
                    add_url: 'logistics/add',
                    edit_url: 'logistics/edit',
                    del_url: 'logistics/del',
                    multi_url: 'logistics/multi',
                    import_url: 'logistics/import',
                    table: 'logistics',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'company', title: __('Company'),visible: false,addClass: "selectpage", extend: "data-source='logisticscompany/index' data-field='company'"},
                        {field: 'company_name', title: __('Company_name'), operate: 'LIKE'},
                        // {field: 'channel', title: __('Channel'),visible: false,addClass: "selectpage", extend: "data-source='logisticschannel/index' data-field='channel'"},
                        {field: 'channel_name', title: __('Channel_name'), operate: 'LIKE'},
                        // {field: 'country', title: __('Country'),visible: false,addClass: "selectpage", extend: "data-source='logisticscountry/index' data-field='country'"},
                        {field: 'country_name', title: __('Country_name'), operate: 'LIKE'},
                        {field: 'min_weight', title: __('Min_weight'), operate:'BETWEEN'},
                        {field: 'max_weight', title: __('Max_weight'), operate:'BETWEEN'},
                        {field: 'freight', title: __('Freight'), operate:'BETWEEN'},
                        {field: 'register', title: __('Register'), operate:'BETWEEN'},
                        {field: 'height_limit', title: __('Height_limit'), operate:'BETWEEN'},
                        {field: 'length_limit', title: __('Length_limit'), operate:'BETWEEN'},
                        {field: 'width_limit', title: __('Width_limit'), operate:'BETWEEN'},
                        // {field: 'admin_id', title: __('管理员'),visible: false,addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        // {field: 'admin_name', title: __('管理员'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(document).on("click", ".btn-company", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("logisticscompany/add?name=" + name, __('添加物流公司'));
            });
            $(document).on("click", ".btn-channel", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("logisticschannel/add?name=" + name, __('添加渠道'));
            });
            $(document).on("click", ".btn-country", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("logisticscountry/add?name=" + name, __('添加国家'));
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                require(['selectpage'], function (SelectPage) {
                    var _displayResults = $.fn.selectPage.Constructor.prototype.displayResults;
                    $.fn.selectPage.Constructor.prototype.displayResults = function (self, json, is_query) {
                        _displayResults.call(this, self, json, is_query);
                        //这里判断是否为分类列表框，也可移除此判断
                        if (self.elem.hidden.is("#c-company")) {
                            //判断查询记录是否为0，也可移除此判断
                            // if (json.originalResult.length == 0) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增物流公司' data-window='top' href='logisticscompany/add'>新增物流公司</a></p><p style='margin:5px;'>（新增物流公司后需要取消下拉框，重新选择）</p>");
                            // }
                        }
                        if (self.elem.hidden.is("#c-channel")) {
                            //判断查询记录是否为0，也可移除此判断
                            // if (json.originalResult.length == 0) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增渠道' data-window='top' href='logisticschannel/add'>新增渠道</a></p><p style='margin:5px;'>（新增渠道后需要取消下拉框，重新选择）</p>");
                            // }
                        }
                        if (self.elem.hidden.is("#c-country")) {
                            //判断查询记录是否为0，也可移除此判断
                            // if (json.originalResult.length == 0) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增国家' data-window='top' href='logisticscountry/add'>新增国家</a></p><p style='margin:5px;'>（新增国家后需要取消下拉框，重新选择）</p>");
                            // }
                        }
                    };
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});